<template>
  <div v-loading="pageLoading" class="home_league">
    <header class="game_modal_header">积分榜</header>
    <div class="schedule_content modal_shadow">
      <div class="gl_pb_15">
        <el-table v-loading="tableLoading" :data="tableData" stripe size="small" style="width: 100%;">
          <el-table-column prop="finalRank" label="排名" align="center" width="80" />
          <el-table-column prop="name" label="姓名" align="left" min-width="160">
            <template slot-scope="scope">
              <div class="agency_wrap gl_clear">
                <div class="agency_photo gl_mr_10">
                  <img :src="getStaticFileUrl(scope.row.photoUrl)" @error="errorPhoto">
                </div>
                <span>{{ scope.row.name }}</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="organization" label="机构名称" min-width="200" />
          <el-table-column label="金牌" align="center" width="80">
            <template slot="header">
              <img style="vertical-align:middle;" src="@/assets/images/league/gold.png">
            </template>
            <template slot-scope="scope">
              <span>{{ scope.row.subRankList[0] ? scope.row.subRankList[0].subRankCount : '' }}</span>
            </template>
          </el-table-column>
          <el-table-column label="银牌" align="center" width="80">
            <template slot="header">
              <img style="vertical-align:middle;" src="@/assets/images/league/silver.png">
            </template>
            <template slot-scope="scope">
              <span>{{ scope.row.subRankList[1] ? scope.row.subRankList[1].subRankCount : '' }}</span>
            </template>
          </el-table-column>
          <el-table-column label="铜牌" align="center" width="80">
            <template slot="header">
              <img style="vertical-align:middle;" src="@/assets/images/league/bronze.png">
            </template>
            <template slot-scope="scope">
              <span>{{ scope.row.subRankList[2] ? scope.row.subRankList[2].subRankCount : '' }}</span>
            </template>
          </el-table-column>
          <el-table-column label="战绩" width="120">
            <template slot-scope="scope">
              {{ scope.row.winCount }} 胜 / {{ scope.row.loseCount }} 负
            </template>
          </el-table-column>
          <el-table-column prop="winPercent" label="胜率" align="center" width="80">
            <template slot-scope="scope">
              {{ (scope.row.winPercent * 100).toString().match(/^\d+(?:\.\d{0,2})?/)[0] + '%' }}
            </template>
          </el-table-column>
          <el-table-column prop="finalPoints" label="联赛积分" align="center" width="80" />
        </el-table>
      </div>

      <!-- 分页 -->
      <el-row style="text-align: center;">
        <el-pagination
          background
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="qo.MaxResultCount"
          :current-page="qo.Page"
          @size-change="changePageSize"
          @current-change="changePage"
        />
      </el-row>
    </div>
  </div>
</template>

<script>
import {
  api_GET_EventsManagementEventResultUnifiedPointRankList
} from '@/extend/api/gameswaggerApi/app';

import {
  Api_GET_EventsManagementEventResultUnifiedPointRankListQO
} from '@/lib/form/game/swaggerForm';

import defaultPhoto from '@/assets/images/face_default.png';

export default {
  props: {
    gameInfo: {
      type: Object,
      default () {
        return {};
      }
    }
  },
  data () {
    return {
      defaultPhoto: defaultPhoto, // 默认照片
      topListType: '', // 榜单类型
      pageLoading: false,
      tableLoading: false,
      tableData: [],
      total: 0,
      qo: new Api_GET_EventsManagementEventResultUnifiedPointRankListQO()
    };
  },
  watch: {
    gameInfo: {
      immediate: true,
      deep: true,
      handler(val) {
        if (val.id) {
          this.getPageList();
        }
      }
    }
  },
  methods: {
    // 获取页面数据
    getPageList (page) {
      const _qo = JSON.parse(JSON.stringify(this.qo));
      _qo.Page = page || 1;
      _qo.SkipCount = (_qo.Page - 1) * _qo.MaxResultCount;
      _qo.CompetitionId = this.gameInfo.id;
      _qo.Type = 'Register';
      _qo.IncludeFinal = true;
      _qo.IncludeSubRank = true;
      delete _qo.EventTypeCode;
      console.log('_qo: ', _qo);
      this.apiRankList(_qo);
    },
    // UnifiedPointRankList
    apiRankList(qo) {
      this.tableLoading = true;
      api_GET_EventsManagementEventResultUnifiedPointRankList(qo)
        .then(
          res => {
            this.tableLoading = false;
            this.total = res.totalCount;
            this.tableData = res.items;
            console.log(res);
          }
        )
        .catch(
          error => {
            this.tableLoading = false;
            console.log(error);
          }
        );
    },
    getStaticFileUrl(relativeUrl) {
      if (!relativeUrl) return '';
      return WEB_CONFIG.VUE_APP_STATIC_FILE_RUL + relativeUrl;
    },
    errorPhoto() {
      const img = event.srcElement;
      img.src = this.defaultPhoto;
      img.onerror = null;
    },
    // 分页
    changePage(page) {
      this.getPageList(page);
    },
    changePageSize(pagesize) {
      this.qo.MaxResultCount = pagesize;
      this.getPageList();
    }
  }
};
</script>
